package com.wust.service.impl;

import com.wust.common.ServerResponse;
import com.wust.dao.CarMapper;
import com.wust.dao.UserCarMapper;
import com.wust.dao.UserMapper;
import com.wust.entity.User;
import com.wust.entity.UserCar;
import com.wust.mwgate.SDKDemo;
import com.wust.service.IUserService;
import com.wust.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by lenovo on 2018/5/16.
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private UserCarMapper userCarMapper;

    //todo 如果存在管理员的话，则需要修改

    @Override
    public ServerResponse<User> login(String username, String password) {
        //先判断用户名存不存在
        int resultCount = userMapper.checkUsername(username);
        if(resultCount == 0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String MD5Password = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectByUsernameAndPassword(username, MD5Password);
        if(user == null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccessMessageAndData("登录成功",user);
    }

    @Override
    public ServerResponse<String> register(User user) {
        //填写个人信息
        //通过手机验证码验证
        if(user.getUsername() == null ||user.getPassword() == null || user.getTelephone() == null){
            return ServerResponse.createByErrorMessage("用户名或密码为空");
        }
        int resultCount = userMapper.checkUsername(user.getUsername());
        if(resultCount > 0){
            return ServerResponse.createByErrorMessage("用户名已被注册");
        }
        resultCount = userMapper.checkTelephone(user.getTelephone());
        if(resultCount > 0){
            return ServerResponse.createByErrorMessage("手机号已被注册");
        }
        user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        resultCount = userMapper.insertSelective(user);
        if(resultCount > 0){
            return ServerResponse.createBySuccessMessage("用户注册成功");
        }
        return ServerResponse.createByErrorMessage("用户注册失败");
    }

    public ServerResponse<User> getUserInfo(String username){
        User user = userMapper.selectByUsername(username);
        if(user == null){
            return ServerResponse.createByErrorMessage("找不到当前用户");
        }
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccessData(user);
    }

    public ServerResponse<Integer> checkPhoneNumber(String telephone){
        int number = (int)((Math.random()*9+1)*100000);
        int result = SDKDemo.sendSingleMessage(number,telephone);
        if(result == 0){
            return ServerResponse.createBySuccessMessageAndData("发送信息成功",number);
        }
        else{
            return ServerResponse.createByErrorMessage("发送信息失败，请重新发送请求");
        }
    }

    @Override
    public ServerResponse<String> bindDevice(int userId ,String matchNumber, String matchCode) {
        int carId = 0;
        //假定了车的匹配WIFI和密码已知
        carId= carMapper.selectByMatchNumberAndCode(matchNumber, matchCode);
        if(carId == 0){
            return ServerResponse.createByErrorMessage("不存在该设备");
        }
        //假定了每个设备只会绑定一个人
        UserCar userCar = new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(userId);
        int resultCount = userCarMapper.insertSelective(userCar);
        if(resultCount > 0){
            return ServerResponse.createBySuccessMessage("绑定成功");
        }
        return ServerResponse.createByErrorMessage("绑定失败");
    }

}
